package com.lsl.service;

import com.alibaba.fastjson.JSON;
import com.lsl.domain.Architect;
import com.lsl.domain.Designer;
import com.lsl.domain.Employee;
import com.lsl.domain.Equipment;
import com.lsl.domain.NoteBook;
import com.lsl.domain.PC;
import com.lsl.domain.Printer;
import com.lsl.domain.Programmer;
import com.lsl.utils.TeamException;

import java.awt.geom.RoundRectangle2D;

/**负责将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法
 * @author lishuailong
 * @create 2020-06-19 16:41
 */
public class NameListService {
   private Employee[] employees;



    /**
     * 为 employees数组及数组元素的初始化
     */
    public NameListService() {
        /*
        1.根据项目提供的Data类构建相应大小的employees数组
        */
        employees = new Employee[Data.EMPLOYEES.length];

        /*2.再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer
        和Architect对象，以及相关联的Equipment子类的对象将对象存于数组中*/

        for (int i = 0; i < employees.length; i++) {
            String[] employee = Data.EMPLOYEES[i];
            //员工的类型
            int type = Integer.parseInt(employee[0]);

            int id = Integer.parseInt(employee[1]);
            String name = employee[2];
            int age =Integer.parseInt(employee[3]);
            double salary = Double.parseDouble(employee[4]);
            Equipment equipment;
            double bonus;
            int stock;

            switch (type){
                case  Data.EMPLOYEE://员工
                    employees[i] = new Employee(id,name,age,salary);
                    break;
                case  Data.PROGRAMMER://程序员
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id,name,age,salary,equipment);
                    break;
                case  Data.DESIGNER://设计师
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(employee[5]);
                    employees[i] = new Designer(id,name,age,salary,equipment,bonus);
                    break;
                case  Data.ARCHITECT://架构师
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(employee[5]);
                    stock= Integer.parseInt(employee[6]);
                    employees[i] = new Architect(id,name,age,salary,equipment,bonus,stock);
                    break;
            }
        }


    }

    /**返回对应的设备对象
     * @param i 数据索引
     * @return
     */
    private Equipment createEquipment(int i) {
        Equipment equipment=null;
        //设备类型
        int type = Integer.parseInt(Data.EQUIPMENTS[i][0]);
        String model = Data.EQUIPMENTS[i][1];
        String x = Data.EQUIPMENTS[i][2];
        switch (type){
            case Data.PC:
                equipment = new PC(model,x);
                break;
            case Data.NOTEBOOK:
                equipment = new NoteBook(model,x);
                break;
            case Data.PRINTER:
                equipment = new Printer(model,x);
                break;
        }
        return equipment;
    }

    /**获取当前的所有员工
     * @return
     */
    public  Employee[] getAllEmployees(){

        return employees;
   }

    /**获取指定ID的员工对象
     * @param id 员工的ID
     * @return
     */
   public Employee getEmployee(int id) throws TeamException {
       for (Employee employee : employees) {
           if(employee.getId()==id){
               return employee;
           }
       }
       throw new TeamException("找不到指定员工");
   }

}
